/**
 * org.assess.oa.system.action.TbDepartmentAction
 */
package org.assess.oa.system.action;

import org.apache.commons.lang.StringUtils;
import org.assess.core.action.BaseAction;
import org.assess.core.service.ServiceResult;
import org.assess.oa.system.bean.TbDepartment;
import org.assess.oa.system.service.TbDepartmentService;

/**
 * @description 部门管理Action
 * @author XiaLongxiang
 * @createtime 2012-5-6 下午10:01:03
 */
public class TbDepartmentAction extends BaseAction {
    /** 序列化号 */
    private static final long serialVersionUID = 2460884269860992864L;
    
    /** 节点的层级（根节点值为0） */
    private String n_level;
    
    /** 用户编码 */
    private String user_code;
    
    private TbDepartment department;
    
    private TbDepartmentService tbDepartmentService;
    
    /**
     * 显示部门管理的JSP页面
     * @return
     */
    public String showDeptJsp() {
        return SUCCESS;
    }
    
    /**
     * 查询部门列表
     * @return
     */
    public String listDepartment() {
        String jsonString = tbDepartmentService.queryTbDepartmentAsList(department);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 加载部门列表
     * @return
     */
    public String listDepartmentAsTreeStr() {
        int depth = StringUtils.isBlank(n_level) ? 0 : Integer.parseInt(n_level) + 1;
        String jsonString = tbDepartmentService.queryTbDepartmentAsTreeStr(depth, department);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 加载部门下拉树所需要的JSON数据
     * @return
     */
    public String listDepartmentAsJsonArray() {
        String jsonString = tbDepartmentService.queryTbDepartmentAsJsonArray(department);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 根据用户编码，查询该用户的所有职能范围信息，并返回相应的JSON字符串
     * @return
     */
    public String listDepartmentAsScopeAll() {
        String jsonString = this.tbDepartmentService.listTbDepartmentAsScopeAll(user_code);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 加载单个部门信息，用于编辑修改页面
     * @return
     */
    public String loadDepartment() {
        if (department != null && StringUtils.isNotBlank(department.getDepartment_code())) {
            super.setEdit(true);
            department = this.tbDepartmentService.queryTbDepartmentByCode(department);
        }
        else {
            super.setEdit(false);
        }
        return SUCCESS;
    }
    
    /**
     * 保存部门信息
     * @return
     */
    public String saveDepartment() {
        try {
            ServiceResult result = this.tbDepartmentService.saveTbDepartment(department, super.isEdit());
            if (result == ServiceResult.save_success) {
                super.setMessage(super.getText("G_SAVE_001", new String[] {super.getText("ST_DEPARTMENT_000")}));
            }
            else {
                super.setMessage(super.getText("G_SAVE_002", new String[] {super.getText("ST_DEPARTMENT_000")}));
            }
        }
        catch (Exception ex) {
            super.setMessage(super.getText("G_SAVE_002", new String[] {super.getText("ST_DEPARTMENT_000")}));
        }
        
        return SUCCESS;
    }
    
    /**
     * 检查部门信息，主要是检查部门编码是否已存在
     * @return
     */
    public String checkDepartment() {
        String jsonString = this.tbDepartmentService.checkTbDepartment(department);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 更改部门的valid_flag属性
     * @return
     */
    public String updateValid() {
        if (department == null) {
            department = new TbDepartment();
        }
        department.setDepartment_code(super.getId());
        try {
            String jsonString = this.tbDepartmentService.updateValid(department);
            super.setJsonString(jsonString);
        }
        catch (Exception ex) {
            
        }
        
        return SUCCESS;
    }
    
    public void setN_level(String n_level) {
        this.n_level = n_level;
    }
    
    public void setUser_code(String user_code) {
        this.user_code = user_code;
    }
    
    public TbDepartment getDepartment() {
        return department;
    }
    
    public void setDepartment(TbDepartment department) {
        this.department = department;
    }
    
    public void setTbDepartmentService(TbDepartmentService tbDepartmentService) {
        this.tbDepartmentService = tbDepartmentService;
    }
}
